package com.smcaiot.jfttask.north.local;

import com.smcaiot.core.exception.ApplicationDomainException;
import com.smcaiot.core.exception.ApplicationInfrastructureException;
import com.smcaiot.core.exception.DomainException;
import com.smcaiot.core.stereotype.Local;
import com.smcaiot.jfttask.domain.Task;
import com.smcaiot.jfttask.domain.TaskBatch;
import com.smcaiot.jfttask.domain.TaskService;
import com.smcaiot.jfttask.north.convert.AddTaskRequestConvert;
import com.smcaiot.jfttask.north.message.AddTaskRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: cz
 * @Date: 2023/9/26
 * @Description:
 */
@Service
@Local
@Slf4j
public class TaskAppService {
    @Autowired
    private TaskService taskService;

    @Transactional(rollbackFor = Exception.class)
    public void addTask(AddTaskRequest request) {
        try {
            Task task = AddTaskRequestConvert.INSTANCE.to(request);
            TaskBatch taskBatch = AddTaskRequestConvert.INSTANCE.toTaskBatch(request);
            taskService.addTask(task, taskBatch, request.getUserId());
        } catch (DomainException ex) {
            throw new ApplicationDomainException(ex.getMessage(), ex);
        } catch (Exception ex) {
            throw new ApplicationInfrastructureException("Infrastructure Error", ex);
        }
    }

}
